package com.sfh.cpc.note.model;

import com.sfh.cpc.com.entity.EMessage;
import com.sfh.cpc.com.entity.ENote;
import com.sfh.cpc.com.entity.EUser;
import com.sfh.cpc.com.entity.RspBase;
import com.sfh.cpc.com.entity.RspPage;
import com.sfh.cpc.com.req.ReqMessage;
import com.sfh.cpc.com.req.ReqNoteAdd;
import com.sfh.cpc.com.req.ReqNoteDel;
import com.sfh.cpc.com.req.ReqNotePageMsg;
import com.sfh.cpc.com.req.ReqNoteSearch;
import com.sfh.cpc.com.req.ReqQueryUser;
import com.sfh.cpc.com.req.ReqUserNote;
import com.sfh.cpc.com.req.ReqUserSearch;
import com.sfh.cpc.com.req.ReqVipConsumeWealth;
import com.sfh.lib.event.RxBusEvent;
import com.sfh.lib.exception.HandleException;
import com.sfh.lib.mvvm.service.BaseViewModel;
import com.sfh.lib.rx.IResult;
import com.sfh.lib.rx.IResultSuccess;

/**
 * 功能描述: TODO
 *
 * @author SunFeihu 孙飞虎
 * @date 2018/8/29
 */
public class NoteModel extends BaseViewModel {



    public void querySearchNote(int nowPage,String content){
        this.execute(new ReqNoteSearch(nowPage, content), new IResult<RspBase<RspPage<ENote>>>() {
            @Override
            public void onFail(HandleException e) {
                setValue("onPageSuccess");
            }

            @Override
            public void onSuccess(RspBase<RspPage<ENote>> rspPageRspBase) throws Exception {

                if (rspPageRspBase.success()) {
                    setValue("onPageSuccess", rspPageRspBase.getData());
                } else {
                    setValue("onPageSuccess");
                }
            }
        });
    }

    public void vipLookNote(long userId){
        this.execute(new ReqVipConsumeWealth(userId));
    }

    /**
     * 查看用户的发布话题【分页】
     * @param nowPage
     * @param userId
     */
    public void pageNote(int nowPage,long userId) {
        this.execute(new ReqUserNote(nowPage, userId), new IResult<RspBase<RspPage<ENote>>>() {
            @Override
            public void onSuccess(RspBase<RspPage<ENote>> o) throws Exception {
                if (o.success()) {
                    setValue("onPageSuccess", o.getData());
                } else {
                    setValue("onPageSuccess");
                }
            }

            @Override
            public void onFail(HandleException e) {
                showToast(e.getMsg());
                setValue("onPageSuccess");
            }
        });
    }

    /***
     * 查看话题留言
     * @param nowPage
     * @param noteId
     */
    public void pageMessage(int nowPage, long noteId) {
        ReqNotePageMsg req = new ReqNotePageMsg();
        req.setPageSize(10);
        req.setNowPage(nowPage);
        req.setNoteId(noteId);
        this.execute(req, new IResult<RspBase<RspPage<EMessage>>>() {
            @Override
            public void onSuccess(RspBase<RspPage<EMessage>> o) throws Exception {
                if (o.success()) {
                    setValue("onPageSuccess", o.getData());
                } else {
                    setValue("onPageSuccess");
                }
            }

            @Override
            public void onFail(HandleException e) {
                showToast(e.getMsg());
                setValue("onPageSuccess");
            }
        });
    }

    public void getUser(long userId){
        this.execute( new ReqQueryUser(userId), new IResult<RspBase<EUser>>() {
            @Override
            public void onSuccess(RspBase<EUser> data) throws Exception {

                if (data.success()){
                    setValue("onShowUser", data.getData());
                }
            }

            @Override
            public void onFail(HandleException e) {
            }
        });
    }

    public void delNote(long noteId){

        this.execute(new ReqNoteDel(noteId), new IResultSuccess<RspBase>() {
            @Override
            public void onSuccess(RspBase rspBase) throws Exception {
                if (rspBase.success()){
                    setValue("onRefreshUI");
                }else{
                    showDialogToast(rspBase.getMsg());
                }
            }
        });
    }

    @RxBusEvent(from = "新增留言成功")
    public void onRefresh(ReqMessage req) {
        setValue("onAddMessageSuccess");
    }

    @RxBusEvent(from = "新增话题成功")
    public void onAddNote(ReqNoteAdd add) {
        setValue("onRefreshUI");
    }

}
